﻿/*****************************************************************************
Copyright (c) 2011 SmartVault, Inc.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
******************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using SmartVault.Net.Http;
using SmartVault.Net;
using SmartVault.Proto;

namespace SmartVault.Rest
{
    /// <summary>
    /// ClientProtocol
    /// 
    /// Provides the operations that an occur once the client has been authenticated.
    /// These operations will always be performed server side as the credentials
    /// present provides wide spread control of the operation your web server
    /// can perform.
    /// 
    /// Delegated Operations (those you perform as a user *may* be performed at 
    /// the client by passing the access token back to your client). 
    /// </summary>
    public class ClientProtocol
    {
        private ClientCfg ClientCfg { get; set; }
        private Uri RootUri { get { return this.ClientCfg.RootUri; } }
        private MediaType MediaType { get { return this.ClientCfg.MediaType; } }
        private Encoding Encoding { get { return this.ClientCfg.Encoding; } }
        private string ClientId { get { return this.ClientCfg.ClientId; } }

        private NetWebClient Client
        {
            get { return new NetWebClient(new WebRequestInfo(this.RootUri, ClientCfg.ClientId, ClientCfg.AccessToken, this.MediaType, this.Encoding)); }
        }

        public ClientProtocol(ClientCfg cfg)
        {
            this.ClientCfg = cfg;

        }

        /// <summary>
        /// StatUser
        /// 
        /// Uses to determine if a user exists and whether or not this client has been authorized to access.
        /// </summary>
        /// <param name="userEmail"></param>
        /// <returns></returns>
        public BasicUserInfoResponse StatUser(string userEmail)
        {
            ClientNetContext ctx = Client.Open(Constants.StatUserInfoPath + "/" + userEmail, HttpMethods.GET.ToString(), null);
            return ctx.ReadPayload<BasicUserInfoResponse>();
        }

        /// <summary>
        /// SignUpNewUser
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public SignUpNewUserResponse SignUpNewUser(SignUpNewUserRequest request)
        {
            ClientNetContext ctx = Client.Open(Constants.SignUpNewUser, HttpMethods.POST.ToString(), null);
            ctx.WritePayload(request);
            return ctx.ReadPayload<SignUpNewUserResponse>();
        }

        /// <summary>
        /// GetDelegationAccessToken
        ///
        /// Create an access token that can be handed back to a client such as a web browser, mobile application, etc.
        /// </summary>
        /// <param name="userEmail"></param>
        /// <returns></returns>
        public UserCfg GetDelegationAccessToken(string userEmail)
        {
            DelegationTokenRequest.Builder cbld = DelegationTokenRequest.CreateBuilder();
            cbld.UserEmail = userEmail;
            ClientNetContext ctx = Client.Open(Constants.DelegationTokenPath, HttpMethods.POST.ToString(), null);
            ctx.WritePayload(cbld.Build());
            DelegationTokenResponse dtoken = ctx.ReadPayload<DelegationTokenResponse>();
            return new UserCfg(userEmail, dtoken, ClientCfg);
        }

        /// <summary>
        /// CreateDelegatedProtocol
        /// 
        /// Create an object that can be used to perform operations as the user. This method is useful if the client operations
        /// are occuring from your web server -- say rather than a web browser.
        /// </summary>
        /// <param name="userEmail">User to impersonate</param>
        /// <returns></returns>
        public DelegatedProtocol CreateDelegatedProtocol(string userEmail)
        {
            return new DelegatedProtocol(GetDelegationAccessToken(userEmail));
        }

        /// <summary>
        /// Request an email is sent to the user
        /// </summary>
        /// <param name="email"></param>
        /// <param name="rqst"></param>
        /// <returns></returns>
        public SendEmailResponse RequestEmail(string userEmail, SendEmailRequest rqst)
        {
            ClientNetContext ctx = Client.Open(Constants.StatUserInfoPath + "/" + userEmail, HttpMethods.POST.ToString(), null);
            ctx.WritePayload(rqst);
            return ctx.ReadPayload<SendEmailResponse>();
        }
    }
}
